﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
    }
});
app.controller('defaultConfig', function ($scope, $http, $timeout) {

    BindGrid();

    function BindGrid() {
        $http.get('Pastors/GetDetails').success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1; //current page
            $scope.entryLimit = 10; //max no of items to display in a page
            $scope.filteredItems = $scope.list.length; //Initially for no filter
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };


    $scope.addUpdate = function (mode, $id, title) {

        $scope.title = title;
        $('#addupdatemodel').modal({ backdrop: 'static' });
        $scope.data = $id;
    }

    $scope.sort_by = function (predicate) {

        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, Id) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                $.ajax({
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify({ DataId: Id }),
                    url: "Pastors/DeleteDetails",
                    success: function (data) {
                        if (data == 'DSUS') {
                            BindGrid();
                            swal("Deleted!", "Details deleted successfully", "success");
                        }
                        else if (data == 'SME') {
                            swal("failed", "Unable to delete this details", "error");
                        }
                    },
                    error: function (status) {
                        swal("failed", "Unable to delete this details", "error");
                    }
                });
            }
        });
    }

    $scope.SaveUpdate = function (data) {
        $scope.requeststatus = true;
        var url = "";
        if (data.KeyId == null) {
            url = "Pastors/InsertDetails";
        }
        else {
            url = "Pastors/UpdateDetails";
        }
        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(data),
            url: url,
            success: function (data) {
                if (data == 'ISUS') {
                    BindGrid();
                    $('#addupdatemodel').modal('hide');
                    messagebox.success("Pastors Details added successfully");
                }
                else if (data == 'SME') {
                    messagebox.error("Sorry, Something went worng please try after sometimes");
                }
                else if (data == 'EIX') {
                    messagebox.warning("Details already exists in your system");
                }
                else if (data == 'USUS') {
                    BindGrid();
                    $('#addupdatemodel').modal('hide');
                    messagebox.success("Pastors updated successfully");
                }
            },
            error: function (status) {
                messagebox.error(status);
            }
        });
    }
});